using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GraphAlgsPresentation.Core
{
    /// <summary>
    /// Color format
    /// </summary>
    public enum ColorFormat
    {
        /// <summary>
        /// Color by name
        /// </summary>
        NamedColor,
        /// <summary>
        /// Color by RGB and alpha
        /// </summary>
        ARGBColor
    }

    /// <summary>
    /// XML Font
    /// </summary>
    public struct XmlFont
    {
        public string FontFamily;
        public GraphicsUnit GraphicsUnit;
        public float Size;
        public FontStyle Style;

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlFont"/> struct.
        /// </summary>
        /// <param name="f">The f.</param>
        public XmlFont(Font f)
        {
            FontFamily = f.FontFamily.Name;
            GraphicsUnit = f.Unit;
            Size = f.Size;
            Style = f.Style;
        }

        /// <summary>
        /// Convert struct XmlFont to the Font.
        /// </summary>
        /// <returns></returns>
        public Font ToFont()
        {
            return new Font(FontFamily, Size, Style,
                GraphicsUnit);
        }
    }

    /// <summary>
    /// The class provides the methods to convert between standard data and xml data
    /// </summary>
    public static class XmlDataConverter
    {
        /// <summary>
        /// Serializes the color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public static string SerializeColor(Color color)
        {
            if (color.IsNamedColor)
                return string.Format("{0}:{1}",
                    ColorFormat.NamedColor, color.Name);
            else
                return string.Format("{0}:{1}:{2}:{3}:{4}",
                    ColorFormat.ARGBColor,
                    color.A, color.R, color.G, color.B);
        }

        /// <summary>
        /// Deserializes the color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public static Color DeserializeColor(string color)
        {
            byte a, r, g, b;

            string[] pieces = color.Split(new char[] { ':' });

            ColorFormat colorType = (ColorFormat)
                Enum.Parse(typeof(ColorFormat), pieces[0], true);

            switch (colorType)
            {
                case ColorFormat.NamedColor:
                    return Color.FromName(pieces[1]);

                case ColorFormat.ARGBColor:
                    a = byte.Parse(pieces[1]);
                    r = byte.Parse(pieces[2]);
                    g = byte.Parse(pieces[3]);
                    b = byte.Parse(pieces[4]);

                    return Color.FromArgb(a, r, g, b);
            }
            return Color.Empty;
        }
    }
}